
using Microsoft.AspNetCore.Mvc;
using System.Net.Http.Headers;
namespace BookShareWebApp
{
    public class DownloadController : Controller
    {
        private readonly BookContext _bookcontext;
        private readonly IWebHostEnvironment _hostingEnvironment;
        public DownloadController(BookContext bookcontext,IWebHostEnvironment hostingEnvironment)
        {
            _bookcontext = bookcontext;
            _hostingEnvironment = hostingEnvironment;
        }

        [HttpGet("/api/download/{filename}")]

        public  async Task<ActionResult>  Download(string filename){
            string uploadsFolder;
            if (string.IsNullOrWhiteSpace(_hostingEnvironment.WebRootPath))
        {
            uploadsFolder = Path.Combine(Directory.GetCurrentDirectory(), "file");
        }
        else{
            uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath,"file");
        }
            
            
            string filePath = Path.Combine(uploadsFolder,filename);
            var bytes =await  System.IO.File.ReadAllBytesAsync(filePath);
          
            
            return File(bytes, "application/pdf", Path.GetFileName(filePath));
            
        }
    }
}